package elitech.vietnam.weatherfashion.tabs.today;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dto.ProductDTO;

public class TodayDetailPagerFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener {

	int page;
	private ProductDTO data;
	private TextView text_productname, text_productprice;
	private ImageView image_product;
	
	public static TodayDetailPagerFragment init (int val, ProductDTO data) {
		TodayDetailPagerFragment frm = new TodayDetailPagerFragment();
		Bundle args = new Bundle();
		args.putInt("page", val);
		frm.setArguments(args);
		frm.data = data;
		return frm;
	}
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		page = getArguments()!=null ? getArguments().getInt("page") : 0;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tab_todaydetail_viewpager, container, false);
		
		text_productname 	= (TextView) view.findViewById(R.id.todaydetail_viewpager_textname);
		text_productprice 	= (TextView) view.findViewById(R.id.todaydetail_viewpager_textprice);
		image_product 		= (ImageView) view.findViewById(R.id.todaydetail_viewpager_image);
		
		initViews();
		return view;
	}
	
	private void initViews() {
		if (data == null)
			return;
		text_productname.setText(data.getTitle());
		text_productprice.setText(data.getPrice() + Utils.getPriceCode());
		
		text_productname.setOnClickListener(this);
		text_productprice.setOnClickListener(this);
		
		Utils.setTextUnderline(text_productname);
		Utils.setTextUnderline(text_productprice);
		//ImageLoader.getInstance().displayImage(AppConfig.SERVER_IMAGE_URL + data.getImage(), image_product);
		UrlImageViewHelper.setUrlDrawable(image_product, Const.SERVER_IMAGE_URL + data.getImage());
		image_product.setOnLongClickListener(this);
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.todaydetail_viewpager_textname:
			Utils.openLink(getActivity(), data.getLink());
			break;

		case R.id.todaydetail_viewpager_textprice:
			Utils.openLink(getActivity(), data.getLink());
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onLongClick(View v) {
		switch (v.getId()) {
		case R.id.todaydetail_viewpager_image:
			Utils.openLink(getActivity(), data.getLink());
			break;

		default:
			break;
		}
		return true;
	}
	
}
